import 'package:flutter_test/flutter_test.dart';
import 'package:pd_log/src/log_file_info.dart';
import 'package:pd_log/src/log_query.dart';
import 'package:pd_log/src/list_options.dart';

void main() {
  group('ListOptions applyOptions', () {
    final files = <PDLogFile>[
      const PDLogFile(
        path: '/root/pd_log/2025/10/01.log',
        sizeBytes: 100,
        modifiedMs: 0,
      ),
      const PDLogFile(
        path: '/root/pd_log/2025/09/30.log',
        sizeBytes: 100,
        modifiedMs: 0,
      ),
      const PDLogFile(
        path: '/root/pd_log/2024/12/31.log',
        sizeBytes: 100,
        modifiedMs: 0, // fallback to parsed date
      ),
      const PDLogFile(
        path: '/root/pd_log/2025/10/02.log',
        sizeBytes: 100,
        modifiedMs: 0,
      ),
    ];

    test('sort by time asc/desc', () {
      final asc = LogQuery.applyOptions(
        files,
        const ListOptions(by: SortBy.time, direction: SortDirection.asc),
      );
      expect(asc.first.fileName, '2024-12-31.log');
      expect(asc.last.fileName, '2025-10-02.log');

      final desc = LogQuery.applyOptions(
        files,
        const ListOptions(by: SortBy.time, direction: SortDirection.desc),
      );
      expect(desc.first.fileName, '2025-10-02.log');
      expect(desc.last.fileName, '2024-12-31.log');
    });

    test('sort by name asc/desc', () {
      final asc = LogQuery.applyOptions(
        files,
        const ListOptions(by: SortBy.name, direction: SortDirection.asc),
      );
      expect(
        asc.map((e) => e.fileName).toList(),
        [
          '2024-12-31.log',
          '2025-09-30.log',
          '2025-10-01.log',
          '2025-10-02.log'
        ],
      );

      final desc = LogQuery.applyOptions(
        files,
        const ListOptions(by: SortBy.name, direction: SortDirection.desc),
      );
      expect(
        desc.map((e) => e.fileName).toList(),
        [
          '2025-10-02.log',
          '2025-10-01.log',
          '2025-09-30.log',
          '2024-12-31.log'
        ],
      );
    });

    test('pagination with pageSize and page', () {
      final paged = LogQuery.applyOptions(
        files,
        const ListOptions(
          by: SortBy.name,
          direction: SortDirection.asc,
          pageSize: 2,
          page: 2,
        ),
      );
      expect(
        paged.map((e) => e.fileName).toList(),
        ['2025-10-01.log', '2025-10-02.log'],
      );
    });
  });
}
